package cn.gailvlun.gll.presentation.emotion.edit

import android.app.Activity
import android.databinding.ObservableBoolean
import android.databinding.ObservableField
import cn.gailvlun.gll.net.emotion.EmotionEnum
import cn.gailvlun.gll.net.emotion.EmotionSource
import cn.gailvlun.gll.net.emotion.EmotionTagReq
import cn.gailvlun.gll.net.emotion.SourceType
import cn.gailvlun.gll.presentation.base.BaseActivity
import cn.gailvlun.gll.presentation.emotion.edit.emotionlib.EXTRA_EMOTION_SOURCE_OBJ
import cn.gailvlun.gll.presentation.emotion.edit.emotionlib.EmotionLibActivity
import com.blankj.utilcode.util.KeyboardUtils
import io.reactivex.functions.Consumer


class EmotionRecordViewModel(private val mActivity: BaseActivity) {

    var emotionTagTitle = ObservableField<String>("")
    var emotionState = ObservableField<EmotionEnum>(EmotionEnum.S)
    var isClick = ObservableBoolean(true)
    var emotionTagReq: EmotionTagReq? = null
    val textObservable = ObservableField<String>("")
    var imageUrlObservable = ObservableField<String>("")

    fun selectEmotionTag() {
        if (KeyboardUtils.isSoftInputVisible(mActivity)) {
            KeyboardUtils.hideSoftInput(mActivity)
        }
        SelectEmotionTagActivity.openActivity(mActivity, Consumer { t ->
            if (t?.resultCode() == Activity.RESULT_OK) {
                val data = t.data()
                val req = data.getParcelableExtra<EmotionTagReq>(EMOTION_TAG_REQ)
                this@EmotionRecordViewModel.emotionTagReq = req
                emotionTagTitle.set(req.title)
                emotionState.set(req.emotionState)
            }
        })
    }


    fun onPickupSource() {

        if (KeyboardUtils.isSoftInputVisible(mActivity)) {
            KeyboardUtils.hideSoftInput(mActivity)
        }
        EmotionLibActivity.openActivity(mActivity, Consumer { t ->
            if (t?.resultCode() == Activity.RESULT_OK) {
                val data = t.data()
                val source = data.getParcelableExtra<EmotionSource>(EXTRA_EMOTION_SOURCE_OBJ)
                when (source.category) {
                    SourceType.S -> {
                        textObservable.set(source.text)
                        KeyboardUtils.showSoftInput(mActivity)
                    }
                    SourceType.E, SourceType.F -> {
                        imageUrlObservable.set(source.pic)

                    }
                    else -> {
                        throw  IllegalArgumentException("no match source type ${source.category}") as Throwable
                    }
                }
            }
        })
    }

}